//
//  Datai1ViewController.m
//  项目二
//
//  Created by rimi on 16/5/19.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "Datai1ViewController.h"

@interface Datai1ViewController ()
/**活动指示器*/
@property(nonatomic,strong)UIActivityIndicatorView *activityIndicatorView;
@end

@implementation Datai1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//      [self initButtonInterface];
    self.title = @"开关、活动指示器";
    [self initUISwitch];
    [self initUIActivityIndicatorView];
}
- (void)initUISwitch{
    UISwitch *switchController = [[UISwitch alloc]init];
    switchController.bounds = CRM(0, 0, 100, 100);
    switchController.center = CPM(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)-200);
    /**开关是否开启*/
    switchController.on = YES;
    /**开关色调*/
    switchController.tintColor = [UIColor blackColor];
    /**开关开启区域的颜色*/
    switchController.onTintColor = [UIColor redColor];
    /**开关按钮的颜色*/
    switchController.thumbTintColor = [UIColor brownColor];
    /**开关事件*/
    [switchController addTarget:self action:@selector(controlProgress:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:switchController];
}

- (void)initButtonInterface{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(200, 100, 80, 30);
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

-(void)initUIActivityIndicatorView{
    //初始化活动指示器
    _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _activityIndicatorView.backgroundColor = [UIColor redColor];
    _activityIndicatorView.bounds = CGRectMake(0, 0, 150, 150);
    _activityIndicatorView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) + 150);
    //当动画停止后是否自动隐藏控件
    _activityIndicatorView.hidesWhenStopped = YES;
    [self.view addSubview:_activityIndicatorView];
}
#pragma mark - UISwitch

-(void)controlProgress:(UISwitch *)sender{
    UISwitch *switchControl = (UISwitch *)sender;
    [_activityIndicatorView startAnimating];
    switchControl.on?[_activityIndicatorView startAnimating]: [_activityIndicatorView stopAnimating];
}


#pragma mark - UIButton
-(void)backButtonTaped:(UIButton *)sender{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}


@end
